home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 22 / Amiga Format AFCD22 (Jan 1998, Issue 106).iso / -in_the_mag- / converters / graphics / netpbm / pbmrexx / rexx / anytopnm.rexx next >
OS/2 REXX Batch file  |  1997-11-16  |  8KB  |  245 lines

  1. /* anytopnm - attempt to convert an unknown type of image file to a P?M file.
  2.  *
  3.  * Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
  4.  * Based on anytopnm (sh-script) by Jef Poskanzer
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation.  This software is provided "as is" without express or
  11.  * implied warranty.
  12.  *
  13.  *  $VER: anytopnm 1.4
  14.  */
  15.  
  16. parse source junk junk progname junk
  17.  
  18. address command
  19. signal on error
  20. signal on break_c
  21. signal on break_d
  22. signal on ioerr
  23. signal on halt
  24. ID = pragma('Id')
  25.  
  26. if ~open(err, "CONSOLE:", 'W') then exit 20
  27.  
  28. if ~show('L', 'rexxsupport.library') then do
  29.     if ~addlib('rexxsupport.library', 0, -30, 0) then exit 20
  30.     end
  31. if ~showlist('A', 'PBMTMP') then 'assign PBMTMP: T:'
  32.  
  33. tmpfiles = ''
  34. i = 0
  35. retval = 0
  36.  
  37. parse arg first tail
  38. if abbrev(first, '-', 1) then call usage
  39. if first = '' | tail ~= '' then call usage
  40.  
  41. orig = first
  42. again = 1
  43. do while again = 1
  44.     tmpfile = 'PBMTMP:atn.'i'.'ID
  45.     i = i+1
  46.     'file' first '>' tmpfile
  47.     if ~open(descfile, tmpfile, 'R') then do
  48.         call writeln err, 'file failed?? - aborting'
  49.         retval = 20
  50.         leave
  51.     end
  52.     desc = upper(readln(descfile))
  53.     call close descfile
  54.     call rm tmpfile
  55.  
  56.     parse var desc junk desc    /* skip filename */
  57.     again = 0
  58.     select
  59.         when pos('PBM', desc) > 0 | pos('PGM', desc) > 0 | pos('PPM', desc) > 0 then do
  60.             'pnmcat -tb' first
  61.             end
  62.         when pos('SUN', desc) > 0 | pos('RASTERFILE', desc) > 0 then do
  63.             'rasttopnm' first
  64.             end
  65.         when pos('GIF', desc) > 0 then do
  66.             'giftopnm' first
  67.             end
  68.         when pos('TIFF', desc) > 0 then do
  69.             'tifftopnm' first
  70.             end
  71.         when pos('IFF', desc) > 0 & pos('ILBM', desc) > 0 then do
  72.             'ilbmtoppm' first
  73.             end
  74.         when pos('IFF', desc) > 0 & pos('RGB8', desc) > 0 then do
  75.             'ilbmtoppm' first
  76.             end
  77.         when pos('IFF', desc) > 0 & pos('RGBN', desc) > 0 then do
  78.             'ilbmtoppm' first
  79.             end
  80.         when pos('IFF', desc) > 0 & pos('PBM ', desc) > 0 then do
  81.             'ilbmtoppm' first
  82.             end
  83.         when pos('LISP', desc) > 0 then do
  84.             'lispmtopgm' first
  85.             end
  86.         when pos('PC', desc) > 0 & pos('PAINTBRUSH', desc) > 0 then do
  87.             'pcxtoppm' first
  88.             end
  89.         when pos('BENNET', desc) > 0 then do
  90.             'ybmtopbm' first
  91.             end
  92.         when pos('SGI', desc) > 0 & pos('IMAGE', desc) > 0 then do
  93.             'sgitopnm' first
  94.             end
  95.         when pos('Utah', desc) > 0 & pos('RLE', desc) > 0 then do
  96.             'rletopnm' first
  97.             end
  98.         when pos('COMPRESS', desc) > 0 then do
  99.             'compress -d <' first '>' tmpfile
  100.             first = tmpfile
  101.             tmpfiles = tmpfiles tmpfile
  102.             again = 1
  103.             end
  104.         when pos('BTOA', desc) > 0 then do
  105.             'atob <' first '>' tmpfile
  106.             first = tmpfile
  107.             tmpfiles = tmpfiles tmpfile
  108.             again = 1
  109.             end
  110.         otherwise do
  111.             /* Can't figure out the file type from the magic number,
  112.                try the extension. */
  113.             desc = upper(orig)
  114.             select
  115.                 when exten(desc, 'PBM') | exten(desc, 'PGM') | exten(desc, 'PPM') then do
  116.                     'pnmcat -tb' first
  117.                     end
  118.                 when exten(desc, 'X') | exten(desc, 'XBM') | exten(desc, 'X10BM') | exten(desc, 'X11BM') | exten(desc, 'BITMAP') then do
  119.                     'xbmtopbm' first
  120.                     end
  121.                 when exten(desc, 'R') | exten(desc, 'RAST') then do
  122.                     'rasttopnm' first
  123.                     end
  124.                 when exten(desc, 'MAC') | exten(desc, 'MACP') then do
  125.                     'macptopbm' first
  126.                     end
  127.                 when exten(desc, 'G3') | exten(desc, 'FAX') then do
  128.                     'g3topbm' first
  129.                     end
  130.                 when exten(desc, 'XWD') | exten(desc, 'X10WD') | exten(desc, 'X11WD') then do
  131.                     'xwdtopnm' first
  132.                     end
  133.                 when exten(desc, 'BRUSH') then do
  134.                     'brushtopbm' first
  135.                     end
  136.                 when exten(desc, 'IMG') then do
  137.                     'gemtopbm' first
  138.                     end
  139.                 when exten(desc, 'PCX') then do
  140.                     'pcxtoppm' first
  141.                     end
  142.                 when exten(desc, 'PIC') | exten(desc, 'PICT') | exten(desc, 'PICT2') then do
  143.                     'picttoppm' first
  144.                     end
  145.                 when exten(desc, 'TIF') | exten(desc, 'TIFF') then do
  146.                     'tifftopnm' first
  147.                     end
  148.                 when exten(desc, 'FS') | exten(desc, 'FACE') then do
  149.                     'fstopgm' first
  150.                     end
  151.                 when exten(desc, 'HIPS') then do
  152.                     'hipstopgm' first
  153.                     end
  154.                 when exten(desc, 'FITS') then do
  155.                     'fitstopnm' first
  156.                     end
  157.                 when exten(desc, 'GIF') then do
  158.                     'giftopnm' first
  159.                     end
  160.                 when exten(desc, 'IFF') | exten(desc, 'ILBM') | exten(desc, 'HAM') | exten(desc, 'HAM8') then do
  161.                     'ilbmtoppm' first
  162.                     end
  163.                 when exten(desc, 'LISPM') then do
  164.                     'lispmtopgm' first
  165.                     end
  166.                 when exten(desc, 'MTV') then do
  167.                     'mtvtoppm' first
  168.                     end
  169.                 when exten(desc, 'QRT') | exten(desc, 'DIS') | exten(desc, 'DUMP') then do  /* POVRay */
  170.                     'qrttoppm' first
  171.                     end
  172.                 when exten(desc, 'TGA') | exten(desc, 'TARGA') then do
  173.                     'tgatoppm' first
  174.                     end
  175.                 when exten(desc, 'XIM') then do
  176.                     'ximtoppm' first
  177.                     end
  178.                 when exten(desc, 'XPM') | exten(desc, 'XPM2') then do
  179.                     'xpmtoppm' first
  180.                     end
  181.                 when exten(desc, 'PI1') then do
  182.                     'pi1toppm' first
  183.                     end
  184.                 when exten(desc, 'PI3') then do
  185.                     'pi3topbm' first
  186.                     end
  187.                 when exten(desc, 'SPU') then do
  188.                     'sputoppm' first
  189.                     end
  190.                 when exten(desc, 'SPC') then do
  191.                     'spctoppm' first
  192.                     end
  193.                 when exten(desc, 'YBM') | exten(desc, 'FACE') then do
  194.                     'ybmtopbm' first
  195.                     end
  196.                 when exten(desc, 'SGI') then do     /* also .RGB and .BW */
  197.                     'sgitopnm' first
  198.                     end
  199.                 when exten(desc, 'RLE') then do
  200.                     'rletopnm' first
  201.                     end
  202.                 otherwise do
  203.                     call writeln(err, orig ': unknown file type')
  204.                     retval = 5
  205.                     end
  206.             end /* select */
  207.         end /* otherwise */
  208.     end /* select */
  209. end /* do */
  210. if tmpfiles ~= '' then do
  211.     call rm tmpfiles
  212. end
  213. exit retval
  214.  
  215. usage:
  216.     call writeln err, 'usage:' progname '<file>'
  217.     exit 10
  218.  
  219. rm: procedure
  220.     arg name
  221.     signal off error    /* ignore WARN */
  222.     'delete' name 'quiet force >NIL:'
  223.     signal on error
  224.     return
  225.  
  226. error:
  227. ioerr:
  228. halt:
  229.     call writeln err, progname ': error at line' SIGL 'code' RC
  230. break_c:
  231. break_d:
  232.     if tmpfiles ~= '' then do
  233.         call rm tmpfiles
  234.     end
  235.     exit 20
  236.  
  237. exten: procedure
  238.     arg name, ext
  239.  
  240.     ext2 = '.'ext
  241.     ext3 = '.'ext'.'
  242.  
  243.     return (pos(ext3, name) > 0 | right(name, length(ext2)) = ext2)
  244.  
  245.